home *** CD-ROM | disk | FTP | other *** search
/ Shareware Super Platinum 8 / Shareware Super Platinum 8.iso / mac / WIN_PRO / DS-1.ZIP;1 / PREPROC.ZIP / FILES.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-10  |  13.2 KB  |  533 lines

  1. /*
  2.  * This file contains routines for setting up characters sources from
  3.  *  files. It contains code to handle the search for include files.
  4.  */
  5. #include "../preproc/preproc.h"
  6. /*
  7.  * The following code is operating-system dependent [@files.01].
  8.  *  System header files needed for handling paths.
  9.  */
  10.  
  11. #if PORT
  12.    /* something may be needed */
  13. Deliberate Syntax Error
  14. #endif                    /* PORT */
  15.  
  16. #if AMIGA || ATARI_ST || MACINTOSH || VM || MVS
  17.    /* something may be needed */
  18. Deliberate Syntax Error
  19. #endif                    /* AMIGA || ATARI_ST || ... */
  20.  
  21. #if MSDOS
  22. #if MICROSOFT || INTEL_386 || HIGHC_386 || ZTC_386
  23.    /* nothing is needed */
  24. #endif                    /* MICROSOFT || INTEL_386 || ... */
  25. #if TURBO
  26. #include <dir.h>
  27. #endif                     /* TURBO */
  28. #endif                    /* MSDOS */
  29.  
  30. #if UNIX || VMS
  31.    /* nothing is needed */
  32. #endif                    /* UNIX || VMS */
  33.  
  34. /*
  35.  * End of operating-system specific code.
  36.  */
  37.  
  38. #include "../preproc/pproto.h"
  39.  
  40. /*
  41.  * Prototype for static function.
  42.  */
  43. hidden novalue file_src Params((char *fname, FILE *f));
  44.  
  45. static char **incl_search; /* standard locations to search for header files */
  46.  
  47. /*
  48.  * file_src - set up the structures for a characters source from a file,
  49.  *  putting the source on the top of the stack.
  50.  */
  51. static novalue file_src(fname, f)
  52. char *fname;
  53. FILE *f;
  54.    {
  55.    union src_ref ref;
  56.  
  57. /*
  58.  * The following code is operating-system dependent [@files.02].
  59.  *  Insure that path syntax is in Unix format for internal consistency
  60.  *  (note, this may not work well on all systems).
  61.  */
  62.  
  63. #if PORT
  64.    /* something may be needed */
  65. Deliberate Syntax Error
  66. #endif                    /* PORT */
  67.  
  68. #if AMIGA || ATARI_ST || MACINTOSH || VM || MVS
  69.    /* something may be needed */
  70. Deliberate Syntax Error
  71. #endif                    /* AMIGA || ATARI_ST || ... */
  72.  
  73. #if MSDOS
  74.    char *s;
  75.    
  76.    /*
  77.     * Convert back slashes to slashes for internal consistency.
  78.     */
  79.    fname = (char *)strdup(fname);
  80.    for (s = fname; *s != '\0'; ++s)
  81.       if (*s == '\\')
  82.          *s = '/';
  83. #endif                    /* MSDOS */
  84.  
  85. #if UNIX || VMS
  86.    /* nothing is needed */
  87. #endif                    /* UNIX || VMS */
  88.  
  89. /*
  90.  * End of operating-system specific code.
  91.  */
  92.  
  93.    ref.cs = new_cs(fname, f, CBufSize);
  94.    push_src(CharSrc, &ref);
  95.    next_char = NULL;
  96.    fill_cbuf();
  97.    }
  98.  
  99. /*
  100.  * source - Open the file named fname or use stdin if fname is "-". fname
  101.  *  is the first file from which to read input (that is, the outermost file).
  102.  */
  103. novalue source(fname)
  104. char *fname;
  105.    {
  106.    FILE *f;
  107.  
  108.    if (strcmp(fname, "-") == 0)
  109.       file_src("<stdin>", stdin);
  110.    else {
  111.       if ((f = fopen(fname, "r")) == NULL) 
  112.          err2("cannot open ", fname);
  113.       file_src(fname, f);
  114.       }
  115.    }
  116.  
  117. /*
  118.  * include - open the file named fname and make it the current input file. 
  119.  */
  120. novalue include(trigger, fname, system)
  121. struct token *trigger;
  122. char *fname;
  123. int system;
  124.    {
  125.    struct str_buf *sbuf;
  126.    char *s;
  127.    char *path;
  128.    char *end_prfx;
  129.    struct src *sp;
  130.    struct char_src *cs;
  131.    char **prefix;
  132.    FILE *f;
  133.  
  134.    /*
  135.     * See if this is an absolute path name.
  136.     */
  137.    if (fname[0] != '/') {
  138.       sbuf = get_sbuf();
  139.       f = NULL;
  140.       if (!system) {
  141.          /*
  142.           * This is not a system include file, so search the locations
  143.           *  of the "ancestor files".
  144.           */
  145.          sp = src_stack;
  146.          while (f == NULL && sp != NULL) {
  147.             if (sp->flag == CharSrc) {
  148.                cs = sp->u.cs;
  149.                if (cs->f != NULL) {
  150.                   /*
  151.                    * This character source is a file.
  152.                    */
  153.                   end_prfx = NULL;
  154.                   for (s = cs->fname; *s != '\0'; ++s)
  155.                      if (*s == '/')
  156.                         end_prfx = s;
  157.                   if (end_prfx != NULL) 
  158.                      for (s = cs->fname; s <= end_prfx; ++s)
  159.                         AppChar(*sbuf, *s);
  160.                   for (s = fname; *s != '\0'; ++s)
  161.                      AppChar(*sbuf, *s);
  162.                   path = str_install(sbuf);
  163.                   f = fopen(path, "r");
  164.                   }
  165.                }
  166.             sp = sp->next;
  167.             }
  168.          }
  169.       /*
  170.        * Search in the locations for the system include files.
  171.        */   
  172.       prefix = incl_search;
  173.       while (f == NULL && *prefix != NULL) {
  174.          for (s = *prefix; *s != '\0'; ++s)
  175.             AppChar(*sbuf, *s);
  176.          if (s > *prefix && s[-1] != '/')
  177.             AppChar(*sbuf, '/');
  178.          for (s = fname; *s != '\0'; ++s)
  179.             AppChar(*sbuf, *s);
  180.          path = str_install(sbuf);
  181.          f = fopen(path, "r");
  182.          prefix = ++prefix;
  183.          }
  184.       rel_sbuf(sbuf);
  185.       }
  186.    else {
  187.       path = fname;
  188.       f = fopen(path, "r");
  189.       }
  190.  
  191.    if (f == NULL)
  192.       errt2(trigger, "cannot open include file ", fname);
  193.    file_src(path, f);
  194.    }
  195.  
  196. /*
  197.  * init_files - Initialize this module, setting up the search path for
  198.  *  system header files.
  199.  */
  200. novalue init_files(opt_lst, opt_args)
  201. char *opt_lst;
  202. char **opt_args;
  203.    {
  204.    int n_paths;
  205.    int i, j;
  206.    char *s, *s1;
  207.   
  208. /*
  209.  * The following code is operating-system dependent [@files.03].
  210.  *  Determine the number of standard locations to search for
  211.  *  header files and provide any declarations needed for the code
  212.  *  that establishes these search locations.
  213.  */
  214.  
  215. #if PORT
  216.    /* probably needs something */
  217. Deliberate Syntax Error
  218. #endif                    /* PORT */
  219.  
  220. #if VMS
  221.    static char *vaxcdir = getenv("VAXC$INCLUDE");
  222.    static char *sysdir = getenv("SYS$LIBRARY");
  223.  
  224.    n_paths = 2;
  225. #endif                    /* VMS */
  226.  
  227. #if AMIGA || ATARI_ST || MACINTOSH || VM || MVS
  228.    /* probably needs something */
  229. Deliberate Syntax Error
  230. #endif                    /* AMIGA || ATARI_ST || ... */
  231.  
  232. #if MSDOS
  233. #if HIGHC_386 || INTEL_386 || ZTC_386
  234.    /* punt for now */
  235.    n_paths = 0;
  236. #endif                    /* HIGHC_386 || INTEL_386 || ... */
  237. #if MICROSOFT
  238.    char *syspath;
  239.    char *cl_var;
  240.    char *incl_var;
  241.    
  242.    incl_var = getenv("INCLUDE");
  243.    cl_var = getenv("CL");
  244.    n_paths = 0;
  245.  
  246.    /*
  247.     * Check the CL environment variable for -I and -X options.
  248.     */
  249.    if (cl_var != NULL) {
  250.       s = cl_var;
  251.       while (*s != '\0') {
  252.          if (*s == '/' || *s == '-') {
  253.             ++s;
  254.             if (*s == 'I') {
  255.                ++n_paths;
  256.                ++s;
  257.                while (*s == ' ' || *s == '\t')
  258.                   ++s;
  259.                while (*s != ' ' && *s != '\t' && *s != '\0')
  260.                   ++s;
  261.                }
  262.             else if (*s == 'X')
  263.                incl_var = NULL;        /* ignore INCLUDE environment var */
  264.             }
  265.          if (*s != '\0')
  266.             ++s;
  267.          }
  268.       }
  269.  
  270.    /*
  271.     * Check the INCLUDE environment variable for standard places to
  272.     *  search.
  273.     */
  274.    if (incl_var == NULL)
  275.       syspath = "";
  276.    else {
  277.       syspath = strdup(incl_var);
  278.       if (*incl_var != '\0')
  279.          ++n_paths;
  280.       while (*incl_var != '\0')
  281.          if (*incl_var++ == ';' && *incl_var != '\0')
  282.             ++n_paths;
  283.       }
  284. #endif                    /* MICROSOFT */
  285.  
  286. #if TURBO
  287.     char *cfg_fname;
  288.     FILE *cfg_file = NULL;
  289.     struct str_buf *sbuf;
  290.     int c;
  291.  
  292.     /*
  293.      * Check the configuration files for -I options.
  294.      */
  295.     n_paths = 0;
  296.     cfg_fname = searchpath("turboc.cfg");
  297.     if (cfg_fname != NULL && (cfg_file = fopen(cfg_fname, "r")) != NULL) {
  298.        c = getc(cfg_file);
  299.        while (c != EOF) {
  300.           if (c == '-') {
  301.              if ((c = getc(cfg_file)) == 'I')
  302.                 ++n_paths;
  303.              }
  304.           else
  305.              c = getc(cfg_file);
  306.           }
  307.        }
  308. #endif                     /* TURBO */
  309.  
  310. #if HIGHC_386 || INTEL_386 || ZTC_386
  311.    /* something may be needed */
  312. #endif                    /* HIGHC_386 || INTEL_386 || ... */
  313. #endif                    /* MSDOS */
  314.  
  315. #if UNIX
  316.    static char *sysdir = "/usr/include/";
  317.  
  318.    n_paths = 1;
  319. #endif                    /* UNIX */
  320.  
  321. /*
  322.  * End of operating-system specific code.
  323.  */
  324.  
  325.    /*
  326.     * Count the number of -I options to the preprocessor.
  327.     */
  328.    for (i = 0; opt_lst[i] != '\0'; ++i)
  329.       if (opt_lst[i] == 'I')
  330.          ++n_paths;
  331.  
  332.    /*
  333.     * Set up the array of standard locations to search for header files.
  334.     */
  335.    incl_search = (char **)alloc((unsigned int)(sizeof(char *)*(n_paths + 1)));
  336.    j = 0;
  337.   
  338. /*
  339.  * The following code is operating-system dependent [@files.04].
  340.  *  Establish the standard locations to search before the -I options
  341.  *  on the preprocessor.
  342.  */
  343.  
  344. #if PORT
  345.    /* something may be needed */
  346. Deliberate Syntax Error
  347. #endif                    /* PORT */
  348.  
  349. #if AMIGA || ATARI_ST || MACINTOSH || VM || MVS
  350.    /* something may be needed */
  351. Deliberate Syntax Error
  352. #endif                    /* AMIGA || ATARI_ST || ... */
  353.  
  354. #if MSDOS
  355. #if MICROSOFT
  356.    /*
  357.     * Get locations from -I options from the CL environment variable.
  358.     */
  359.    if (cl_var != NULL)
  360.       while (*cl_var != '\0') {
  361.          if (*cl_var == '/' || *cl_var == '-') {
  362.             ++cl_var;
  363.             if (*cl_var == 'I') {
  364.                   ++cl_var;
  365.                   while (*cl_var == ' ' || *cl_var == '\t')
  366.                      ++cl_var;
  367.                   i = 0;
  368.                   while (cl_var[i] != ' ' && cl_var[i] != '\t' &&
  369.                     cl_var[i] != '\0')
  370.                      ++i;
  371.                   s1 = (char *) alloc((unsigned int)(i + 1));
  372.                   strncpy(s1, cl_var, i);
  373.                   s1[i] = '\0';
  374.                   /*
  375.                    * Convert back slashes to slashes for internal consistency.
  376.                    */
  377.                   for (s = s1; *s != '\0'; ++s)
  378.                      if (*s == '\\')
  379.                         *s = '/';
  380.                   incl_search[j++] = s1;
  381.                   cl_var += i;
  382.                }
  383.             }
  384.          if (*cl_var != '\0')
  385.             ++cl_var;
  386.          }
  387. #endif                    /* MICROSOFT */
  388.  
  389. #if HIGHC_386 || INTEL_386 || ZTC_386
  390.    /* something is needed */
  391. #endif                    /* HIGHC_386 || INTEL_386 || ZTC_386 */
  392. #endif                    /* MSDOS */
  393.  
  394. #if UNIX || VMS
  395.    /* nothing is needed */
  396. #endif                    /* UNIX || VMS */
  397.  
  398. /*
  399.  * End of operating-system specific code.
  400.  */
  401.  
  402.    /*
  403.     * Get the locations from the -I options to the preprocessor.
  404.     */
  405.    for (i = 0; opt_lst[i] != '\0'; ++i)
  406.       if (opt_lst[i] == 'I') {
  407.          s = opt_args[i];
  408.          s1 = (char *) alloc((unsigned int)(strlen(s)+1));
  409.          strcpy(s1, s);
  410.          
  411. /*
  412.  * The following code is operating-system dependent [@files.05].
  413.  *  Insure that path syntax is in Unix format for internal consistency
  414.  *  (note, this may not work well on all systems).
  415.  */
  416.  
  417. #if PORT
  418.    /* something might be needed */
  419. Deliberate Syntax Error
  420. #endif                    /* PORT */
  421.  
  422. #if AMIGA || ATARI_ST || MACINTOSH || VM || MVS
  423.    /* something might be needed */
  424. Deliberate Syntax Error
  425. #endif                    /* AMIGA || ATARI_ST || ... */
  426.  
  427. #if MSDOS
  428.          /*
  429.           * Convert back slashes to slashes for internal consistency.
  430.           */
  431.          for (s = s1; *s != '\0'; ++s)
  432.             if (*s == '\\')
  433.                *s = '/';
  434. #endif                    /* MSDOS */
  435.  
  436. #if UNIX || VMS
  437.    /* nothing is needed */
  438. #endif                    /* UNIX || VMS */
  439.  
  440. /*
  441.  * End of operating-system specific code.
  442.  */
  443.          
  444.          incl_search[j++] = s1;
  445.          }
  446.  
  447. /*
  448.  * The following code is operating-system dependent [@files.06].
  449.  *  Establish the standard locations to search after the -I options
  450.  *  on the preprocessor.
  451.  */
  452.  
  453. #if PORT
  454.    /* probably needs something */
  455. Deliberate Syntax Error
  456. #endif                    /* PORT */
  457.  
  458. #if VMS
  459.    incl_search[n_paths - 1] = sysdir;
  460.    incl_search[n_paths - 2] = vaxcdir;
  461. #endif                    /* VMS */
  462.  
  463. #if AMIGA || ATARI_ST || MACINTOSH || VM || MVS
  464.    /* probably needs something */
  465. Deliberate Syntax Error
  466. #endif                    /* AMIGA || ATARI_ST || ... */
  467.  
  468. #if MSDOS
  469. #if MICROSOFT
  470.    /*
  471.     * Get the locations from the INCLUDE environment variable.
  472.     */
  473.    s = syspath;
  474.    if (*s != '\0')
  475.       incl_search[j++] = s;
  476.    while (*s != '\0') {
  477.       if (*s == ';') {
  478.          *s = '\0';
  479.          ++s;
  480.          if (*s != '\0')
  481.             incl_search[j++] = s;
  482.          }
  483.       else {
  484.          if (*s == '\\')
  485.             *s = '/';
  486.          ++s;
  487.          }
  488.       }
  489. #endif                    /* MICROSOFT */
  490.  
  491. #if TURBO
  492.     /*
  493.      * Get the locations from the -I options in the configuration file.
  494.      */
  495.     if (cfg_file != NULL) {
  496.        rewind(cfg_file);
  497.        sbuf = get_sbuf();
  498.        c = getc(cfg_file);
  499.        while (c != EOF) {
  500.           if (c == '-') {
  501.              if ((c = getc(cfg_file)) == 'I') {
  502.                 c = getc(cfg_file);
  503.                 while (c != ' ' && c != '\t' && c != '\n' && c != EOF) {
  504.                    AppChar(*sbuf, c);
  505.                    c = getc(cfg_file);
  506.                    }
  507.                 incl_search[j++] = str_install(sbuf);
  508.                 }
  509.              }
  510.           else
  511.              c = getc(cfg_file);
  512.           }
  513.        rel_sbuf(sbuf);
  514.        fclose(cfg_file);
  515.        }
  516. #endif                     /* TURBO */
  517.  
  518. #if HIGHC_386 || INTEL_386 || ZTC_386
  519.   /* something is needed */
  520. #endif                    /* HIGHC_386 || INTEL_386 || ZTC_386 */
  521. #endif                    /* MSDOS */
  522.  
  523. #if UNIX
  524.    incl_search[n_paths - 1] = sysdir;
  525. #endif                    /* UNIX */
  526.  
  527. /*
  528.  * End of operating-system specific code.
  529.  */
  530.  
  531.    incl_search[n_paths] = NULL;
  532.    }
  533.